package com.jay.nio.clazz;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.Arrays;

public class LongBufferCls {
	public static void main(String[] args) throws IOException {
		// 创建一个capacity为256的ByteBuffer
		LongBuffer buf = LongBuffer.allocate(256);
		System.out.println("is direct? " + buf.isDirect());
		System.out.println(buf.array().length);
		System.out.println(buf.hasArray());
		System.out.println(buf.isReadOnly());
		System.out.println(buf.order());

		System.out.println("-----------------------------");
		buf.put(new long[] { 111, 222, 333, 444, 555, 666, 7777 });
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		long[] content = new long[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		System.out.println(buf.arrayOffset());

		buf.clear();

		System.out.println("-----------------------------");
		buf.put(3213);
		buf.put(5345435);
		// rewind 不重设limit，所以以前剩余的字符也会输出
		// buf.rewind();
		buf.flip();
		content = new long[buf.limit()];
		buf.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		buf.clear();
		System.out.println("-----------------------------");
		buf.position(2);
		buf.put(453);
		buf.put(7673);
		buf.put(2312434);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		buf.flip();
		buf.position(2);
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());
		// 比较复杂，可参见实现代码
		buf.compact();
		System.out.println(buf.position() + "/" + buf.limit() + "/"
				+ buf.capacity());

		System.out.println("-----------------------------");
		// 复制
		LongBuffer bufDup = buf.duplicate();
		System.out.println(bufDup.position() + "/" + bufDup.limit() + "/"
				+ bufDup.capacity());

		// position后面的子buff
		LongBuffer bufSlice = buf.slice();
		System.out.println(bufSlice.position() + "/" + bufSlice.limit() + "/"
				+ bufSlice.capacity());

		// position后面的子buff
		LongBuffer bufWrap = LongBuffer.wrap(new long[] { 111, 222, 333, 444,
				555, 666, 7777 });
		System.out.println(bufWrap.position() + "/" + bufWrap.limit() + "/"
				+ bufWrap.capacity());

		System.out.println("-----------------------------");
		LongBuffer bufD = ByteBuffer.allocateDirect(256).asLongBuffer();
		System.out.println("is direct? " + bufD.isDirect());
		System.out.println(bufD.hasArray());
		System.out.println(bufD.isReadOnly());
		System.out.println(bufD.order());

		buf.put(new long[] { 111, 222, 333, 444, 555, 666, 7777 });
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());
		bufD.flip();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		content = new long[bufD.limit()];
		bufD.get(content);
		// 把byte数组的内容写到标准输出
		System.out.print(Arrays.toString(content));
		System.out.println();
		System.out.println(bufD.position() + "/" + bufD.limit() + "/"
				+ bufD.capacity());

		bufD.clear();
	}

}
